# from selenium import webdriver
#
#
#
# def get_info(url):
#     option = webdriver.ChromeOptions()
#     option.add_argument('headless')
#     option.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
#     driver = webdriver.Chrome(options=option)
#
#     # 隐式等待10秒
#     driver.implicitly_wait(2)
#     # driver.get("https://www.cvedetails.com/cve/CVE-2022-30525")
#     driver.get(url)
#     # 窗口最大化
#     driver.minimize_window()
#
#     vulnerability_details = driver.find_element_by_xpath(
#         '/html/body/table/tbody/tr[2]/td[2]/div/table/tbody/tr[1]/td[1]/h1/a')
#     scoreTable_elements = driver.find_element_by_id("cvssscorestable")
#     references_element = driver.find_element_by_id("vulnrefstable")
#
#     scores_type_dict = {}
#     references_list = []
#     source_datas = []
#     number = ''
#
#     try:
#         # 漏洞编号的获取
#         number = vulnerability_details.text
#         scores_type_dict["CVE编号"] = number
#         # CVSS Scores & Vulnerability Types的处理
#         for i in range(9):
#             # css_Tr_Th
#             thPath = 'tbody/tr[' + str(i + 1) + ']/th'
#             scores_key = scoreTable_elements.find_element_by_xpath(thPath).text
#             # css_Tr_Td
#             tdPath = 'tbody/tr[' + str(i + 1) + ']/td'
#             scores_value = scoreTable_elements.find_element_by_xpath(tdPath).text
#             scores_type_dict[scores_key] = scores_value
#
#         # References For CVE-2022-26355的处理
#         ref_size = len(references_element.find_elements_by_xpath('tbody/tr'))
#
#         for i in range(ref_size):
#             path = 'tbody/tr[' + str(i + 1) + ']'
#             references_list.append(references_element.find_element_by_xpath(path).text)
#         header1 = ['CVE编号']
#         link_dict = {'CVE编号': number}
#         length = len(references_list)
#
#         for i in range(10):
#             key = 'url' + str(i + 1)
#             header1.append(key)
#             if i < length:
#                 link_dict[key] = references_list[i]
#             else:
#                 link_dict[key] = " "
#         print(number)
#         return scores_type_dict, link_dict
#         # with open(score_file_name, "w+", encoding='utf-8', newline='') as f:
#         #     writer = csv.writer(f)
#         #     for k, v in scores_type_dict.items():
#         #         writer.writerow([k, v])
#     except:
#         pass
#     finally:
#         driver.close()
from selenium import webdriver
import csv
import os.path as hasOrNo


def get_info(url):
    try:
        option = webdriver.ChromeOptions()
        option.add_argument('headless')
        option.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
        driver = webdriver.Chrome(options=option)

        # 隐式等待10秒
        driver.implicitly_wait(2)
        # driver.get("https://www.cvedetails.com/cve/CVE-2022-30525")
        driver.get(url)
        # 窗口最大化
        driver.minimize_window()

        vulnerability_details = driver.find_element_by_xpath(
            '/html/body/table/tbody/tr[2]/td[2]/div/table/tbody/tr[1]/td[1]/h1/a')
        scoreTable_elements = driver.find_element_by_id("cvssscorestable")
        references_element = driver.find_element_by_id("vulnrefstable")

        scores_type_dict = {}
        references_list = []
        source_datas = []
        link_datas = []
        number = ''
        # 漏洞编号的获取
        number = vulnerability_details.text
        scores_type_dict["CVE编号"] = number
        prefix = "cve/" + number[:8]
        # CVSS Scores & Vulnerability Types的处理
        for i in range(9):
            # css_Tr_Th
            thPath = 'tbody/tr[' + str(i + 1) + ']/th'
            scores_key = scoreTable_elements.find_element_by_xpath(thPath).text
            # css_Tr_Td
            tdPath = 'tbody/tr[' + str(i + 1) + ']/td'
            scores_value = scoreTable_elements.find_element_by_xpath(tdPath).text
            scores_type_dict[scores_key] = scores_value

        # References For CVE-2022-26355的处理
        ref_size = len(references_element.find_elements_by_xpath('tbody/tr'))

        for i in range(ref_size):
            path = 'tbody/tr[' + str(i + 1) + ']'
            references_list.append(references_element.find_element_by_xpath(path).text)
        # print('漏洞编号: ' + number)
        # print(scores_type_dict)
        # print(references_list)

        header1 = ['漏洞编号']
        link_dict = {'漏洞编号': number}
        length = len(references_list)

        for i in range(10):
            key = 'url' + str(i + 1)
            header1.append(key)
            if i < length:
                link_dict[key] = references_list[i]
            else:
                link_dict[key] = " "

        score_file_name = prefix
        header = list(scores_type_dict.keys())
        source_datas.append(scores_type_dict)
        link_datas.append(link_dict)

        refer_file_name = prefix + "_" + "links"
        if hasOrNo.exists(refer_file_name):
            with open(refer_file_name, "a+", encoding='utf-8', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=header1)
                writer.writerows(link_datas)
        else:
            with open(refer_file_name, "a+", encoding='utf-8', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=header1)
                writer.writeheader()
                writer.writerows(link_datas)

        if hasOrNo.exists(score_file_name):
            with open(score_file_name, "a+", encoding='utf-8', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=header)
                writer.writerows(source_datas)
        else:
            with open(score_file_name, "a+", encoding='utf-8', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=header)
                writer.writeheader()
                writer.writerows(source_datas)

        return scores_type_dict, link_dict
        # with open(score_file_name, "w+", encoding='utf-8', newline='') as f:
        #     writer = csv.writer(f)
        #     for k, v in scores_type_dict.items():
        #         writer.writerow([k, v])
    except:
        with open("error.txt","w+",encoding='utf-8',newline='') as f:
            f.write(url)
    finally:
        driver.close()

